package com.zsc.shixun.controller;


import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.config.UserSecurity;
import com.zsc.shixun.model.user.UserUpdateVO;
import com.zsc.shixun.model.user.UserVO;
import com.zsc.shixun.service.IUserService;
import com.zsc.shixun.utils.FIleUtils;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 个人资料 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-06
 */
@Validated
@Controller
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService userService;
    @Value("${my.upload.imgPath}")
    public String path;

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    public FIleUtils fIleUtils;

    @Autowired
    public LoginMessageUtils loginMessageUtils;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {

        UserSecurity principal = loginMessageUtils.getMessage();
        Long id = principal.getLoginUser().getId();
        UserVO userVO = userService.findById(id);
        request.setAttribute("userVO", userVO);

        return "admin/user/index";
    }


//    /**
//     * 头像上传
//     *
//     * @param fileUpload
//     * @return
//     */
////    @PostMapping("/uploadFile")
////    @ResponseBody
////    public CommonResult uploadFile(MultipartFile fileUpload,@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
////        String dirPath=null;
////        String fileName = fileUpload.getOriginalFilename();
////        fileName = UUID.randomUUID() + "_" + fileName;
////        System.out.println(path);
////        String fullPath = fIleUtils.upload(path, fileName, fileUpload);
////        userService.updateByImg(id, fullPath);
////        return ResultUtil.success("文件上传成功，亲！");
////    }

    /**
     * 头像上传
     *
     * @param request
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    @Secured(value = "ROLE_user_update")
    public CommonResult uploadFile(HttpServletRequest request) {  //获取参数
        String id = request.getParameter("id");

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> fileList = multipartRequest.getFiles("file-Portrait");


        for (MultipartFile file : fileList) {
            String fileName = file.getOriginalFilename();
            fileName = UUID.randomUUID() + "_" + fileName;
            System.out.println(path);
            String fullPath = fIleUtils.upload(path, fileName, file);
            userService.updateByImg(Long.parseLong(id), fullPath);
        }
        return ResultUtil.success("文件上传成功，亲！");
    }

    /**
     * 修改个人资料
     *
     * @param userUpdateVO
     * @return
     */
    @PostMapping("/updateUser")
    @ResponseBody
    @Secured(value = "ROLE_user_update")
    public CommonResult updateUser(@Validated @ModelAttribute UserUpdateVO userUpdateVO) {
        userService.updateUser(userUpdateVO, loginMessageUtils.getMessage().getLoginUser().getUsername());
        return ResultUtil.success("修改资料成功，亲！");
    }
}
